package edu.pw.elka.gtsna.graph_algorithms;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import edu.pw.elka.gtsna.graph.Graph;



public class CheckConnectivity {
	/* Graph where we want to find articulation (pivotal) points */
	Graph graph;
	
	/* The connected component of the graph to which we are restricted */
	Set<Integer> subgraph;
		
	Boolean isConnected;
	
	/* if node was visited */
	Map <Integer, Boolean> visited;
	int visitedNumber;
	
	Stack<Integer> stack;
	Set<Integer> component = new LinkedHashSet<Integer>();
	
	public Set<Integer>  getComponent(){
		return new LinkedHashSet<Integer>(component);
	}
	
	public CheckConnectivity(Graph graph){
		this.graph = graph;
		visited = new HashMap<Integer, Boolean>();
		stack = new Stack<Integer>();
	}
	
	
	public void check(Set<Integer> subgraph){
		this.subgraph = subgraph;
		isConnected = false;
		visitedNumber=0;
		stack.clear();
		visited.clear();
		component.clear();
		for(int v: subgraph){
			visited.put(v, false);
		}
		
		int root = subgraph.iterator().next();
		stack.push(root);
		visited.put(root, true);
		while (!stack.isEmpty()) {
			int u = stack.pop();
			component.add(u);
			visitedNumber++;
			for (int i: graph.getNeigbours(u)) {
				if (subgraph.contains(i) && !visited.get(i)){
					visited.put(i, true);
					stack.push(i);
				}
			}
		}
		isConnected = (visitedNumber==subgraph.size());
		
	}

	
	public Boolean isConencted(){
		return isConnected;
	}
	
	
}
